﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpGenericTeach
{
    /// <summary>
    /// 一、
    /// 
    /// 
    /// 
    /// 二、泛型类、泛型参数、泛型返回值
    /// 
    /// 
    /// 三、泛型中default关键字
    /// 
    /// 
    /// 
    /// 四、泛型约束和动态类型dynamic
    /// 
    /// 
    /// 
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            //Test1();

            Test3();
            Console.ReadLine();
        }

        static void Test1()
        {
            //创建泛型类对象
            MyStack<int> stack1 = new MyStack<int>(5);

            //元素入栈
            stack1.Push(1);
            stack1.Push(2);
            stack1.Push(3);
            stack1.Push(4);
            stack1.Push(5);

            //元素出栈
            Console.WriteLine(stack1.Pop());
            Console.WriteLine(stack1.Pop());
            Console.WriteLine(stack1.Pop());
            Console.WriteLine(stack1.Pop());
            Console.WriteLine(stack1.Pop());


            //创建泛型类对象
            MyStack<string> stack2 = new MyStack<string>(5);

            //元素入栈
            stack2.Push("工控上位机");
            stack2.Push("运动控制上位机");
            stack2.Push("视觉上位机");
            stack2.Push("WTM上位机");
            stack2.Push("高级上位机");

            //元素出栈
            Console.WriteLine(stack2.Pop());
            Console.WriteLine(stack2.Pop());
            Console.WriteLine(stack2.Pop());
            Console.WriteLine(stack2.Pop());
            Console.WriteLine(stack2.Pop());
        }

        static void Test2()
        {
            MyGenericClass1<int, string> myClass = new MyGenericClass1<int, string>();
        }

        static void Test3()
        {
            MyGenericClass2<int, Course, Teacher> myClass = new MyGenericClass2<int, Course, Teacher>();

            //给对象属性赋值
            myClass.Publisher = new Teacher { Name = "Test", CourseCount = 5 };

            myClass.CountList = new List<Course>
            {
                new Course(){CourseName = "Test1", Period=3},
                new Course(){CourseName = "Test2", Period=4},
                new Course(){CourseName = "Test3", Period=5},
            };

            //方法调用
            var myCourse = myClass.BuyCourse(0);

            Console.WriteLine(myCourse.CourseName);
            Console.WriteLine(myCourse.Period);

        }

        public static int Add(int a, int b)
        {
            return a + b;
        }

        public static Double Add(double a, double b)
        {
            return a + b;
        }

        /// <summary>
        /// 泛型约束和dynamic动态类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static T Add1<T,T1,T2>(T1 a, T2 b) where T : struct,T1,T2
        {
            //retutn a + b; //这个直接使用是错误的
            dynamic a1 = a;
            dynamic b1 = b;
            return a1 + b1;
        }

        static void Test4()
        {
            double result1 = Add1<int, int, int>(10, 20);

            double result = Add1<double, double, double>(10.9, 20);
        }
    }
}
